[20220824_173045_user] D2_main.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /* USER CODE BEGIN Header */
  2. /**
  3. ******************************************************************************
  4. * @file : main.c
  5. * @brief : Main program body
  6. * @attention
  7. *
  8. * <h2><center>&copy; Copyright (c) 2019 STMicroelectronics.
  9. * All rights reserved.</center></h2>
  10. *
  11. * This software component is licensed by ST under BSD 3-Clause license,
  12. * the License; You may not use this file except in compliance with the
  13. * License. You may obtain a copy of the License at:
  14. * www.st.com/SLA0044
  15. *
  16. ******************************************************************************
  17. */
  18. /* USER CODE END Header */
  19. /* Includes ------------------------------------------------------------------*/
  20. #include "main.h"
  21. #include "adc.h"
  22. #include "usart.h"
  23. #include "i2c.h"
  24. #include "tim.h"
  25. #include "stm32f4xx_hal.h"
  26. #include "gpio.h"
  27. #include "stdio.h"
  28. /* Private variables ---------------------------------------------------------*/
  29. /* Private variables ---------------------------------------------------------*/
  30. #define VECT_TAB_OFFSET 0x10000
  31. int tankstatus = 0;
  32. uint8_t zigbeecmd1[5]={0xFE,0x00,0x01,0x00,0x01};
  33. uint8_t zigbeecmd2[5]={0xFE,0x00,0x02,0x00,0x02};
  34. uint8_t zigbeestatu[20];
  35. uint8_t zigbeestatu1[5];
  36. uint8_t zigbeecon[7]="connect";
  37. uint8_t zigbeenotcon[11]="not connect";
  38. uint8_t zigbeechang[14]="change is down";
  39. uint16_t M4_AD_Value = 0;
  40. float M4_voltage_V =0;
  41. float M4_pH_mid = 1.500;
  42. float M4_pH_low = 2.030;
  43. float M4_pH_high =0.975;
  44. float M4_PH=0;
  45. /* USER CODE BEGIN PV */
  46. typedef void (*pFunction)(void);
  47. /* USER CODE END PV */
  48. /* Private function prototypes -----------------------------------------------*/
  49. void SystemClock_Config(void);
  50. void MX_ADC1_Init1(char pin);
  51. void sensor(void);
  52. /* USER CODE BEGIN PFP */
  53. /* USER CODE END PFP */
  54. /* Private user code ---------------------------------------------------------*/
  55. /* USER CODE BEGIN 0 */
  56. int _write(int fd, char *ch, int len)
  57. {
  58. HAL_UART_Transmit(&huart4, (uint8_t*)ch, len, 0xFFFF);
  59. HAL_UART_Transmit(&huart2, (uint8_t*)ch, len, 0xFFFF);
  60. return len;
  61. }
  62. void print(char* s, float c)
  63. { int w;
  64. int w_d;
  65. int w_f;
  66. if(c<0)
  67. {
  68. w = c*100;
  69. w_d = w/100;
  70. w_f = (-1*w)%100;
  71. }
  72. else
  73. {
  74. w = c*100;
  75. w_d = w/100;
  76. w_f = w%100;
  77. }
  78. printf("%s = %d.%d\n",s,w_d,w_f);
  79. }
  80. /* USER CODE END 0 */
  81. /**
  82. * @brief The application entry point.
  83. * @retval int
  84. */
  85. int main(void)
  86. {
  87. SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET;
  88. HAL_Init();
  89. SystemClock_Config();
  90. MX_GPIO_Init();
  91. MX_USART2_UART_Init();
  92. MX_UART4_Init();
  93. MX_USART1_UART_Init();
  94. HAL_UART_Transmit(&huart2, (uint8_t *)zigbeecmd2, sizeof(zigbeecmd2), 10);
  95. HAL_Delay(1000);
  96. HAL_UART_Transmit(&huart2, (uint8_t *)zigbeecmd1, sizeof(zigbeecmd1), 10);
  97. HAL_UART_Receive(&huart2, (uint8_t *)zigbeestatu,sizeof(zigbeestatu),2000);
  98. HAL_UART_Transmit(&huart4, (uint8_t *)zigbeestatu, sizeof(zigbeestatu),10);
  99. if (zigbeestatu[18]== 0x01 || zigbeestatu[18]== 0x02)
  100. {
  101. HAL_UART_Transmit(&huart4, (uint8_t *)zigbeecon, sizeof(zigbeecon),10);
  102. }
  103. else if(zigbeestatu[18]== 0x05)
  104. {
  105. HAL_UART_Transmit(&huart4, (uint8_t *)zigbeenotcon, sizeof(zigbeenotcon),10);
  106. }
  107. while (1)
  108. {
  109. sensor();
  110. if(M4_PH<16)
  111. {
  112. HAL_GPIO_WritePin(GPIOA,GPIO_PIN_13, GPIO_PIN_RESET);
  113. HAL_GPIO_WritePin(GPIOA,GPIO_PIN_8, GPIO_PIN_RESET);
  114. HAL_GPIO_WritePin(GPIOD,GPIO_PIN_15, GPIO_PIN_RESET);
  115. HAL_GPIO_WritePin(GPIOD,GPIO_PIN_13, GPIO_PIN_RESET);
  116. HAL_GPIO_WritePin(GPIOD,GPIO_PIN_11, GPIO_PIN_RESET);
  117. HAL_GPIO_WritePin(GPIOB,GPIO_PIN_15, GPIO_PIN_RESET);
  118. HAL_GPIO_WritePin(GPIOE,GPIO_PIN_15, GPIO_PIN_RESET);
  119. HAL_GPIO_WritePin(GPIOE,GPIO_PIN_11, GPIO_PIN_RESET);
  120. HAL_Delay(5000);
  121. HAL_GPIO_WritePin(GPIOA,GPIO_PIN_13, GPIO_PIN_SET);
  122. }
  123. }
  124. }
  125. void SystemClock_Config(void)
  126. {
  127. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  128. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  129. __HAL_RCC_PWR_CLK_ENABLE();
  130. __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
  131. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  132. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  133. RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  134. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  135. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
  136. RCC_OscInitStruct.PLL.PLLM = 8;
  137. RCC_OscInitStruct.PLL.PLLN = 72;
  138. RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  139. RCC_OscInitStruct.PLL.PLLQ = 3;
  140. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  141. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  142. {
  143. Error_Handler();
  144. }
  145. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  146. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  147. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  148. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  149. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  150. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  151. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  152. {
  153. Error_Handler();
  154. }
  155. }
  156. void user_pwm_setvalue(uint16_t value)
  157. {
  158. TIM_OC_InitTypeDef sConfigOC;
  159. sConfigOC.OCMode = TIM_OCMODE_PWM1;
  160. sConfigOC.Pulse = value;
  161. sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
  162. sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
  163. HAL_TIM_PWM_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_1);
  164. HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_1);
  165. }
  166. void user_pwm_setvalue2(uint16_t value)
  167. {
  168. TIM_OC_InitTypeDef sConfigOC;
  169. sConfigOC.OCMode = TIM_OCMODE_PWM1;
  170. sConfigOC.Pulse = value;
  171. sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
  172. sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
  173. HAL_TIM_PWM_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_2);
  174. HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_2);
  175. }
  176. void sensor(void)
  177. {
  178. MX_ADC1_Init1(ADC_CHANNEL_0);
  179. HAL_ADC_Start(&hadc1);
  180. HAL_ADC_PollForConversion(&hadc1, 50);
  181. if(HAL_IS_BIT_SET(HAL_ADC_GetState(&hadc1), HAL_ADC_STATE_REG_EOC))
  182. {
  183. M4_AD_Value = HAL_ADC_GetValue(&hadc1);
  184. M4_voltage_V = M4_AD_Value*3.3f/4096;
  185. if (M4_voltage_V > M4_pH_mid)
  186. {
  187. M4_PH = 7.0 - 3.0 / (M4_pH_low - M4_pH_mid) * (M4_voltage_V - M4_pH_mid);
  188. }
  189. else
  190. {
  191. M4_PH = 7.0 - 3.0 / (M4_pH_mid - M4_pH_high) * (M4_voltage_V - M4_pH_mid);
  192. }
  193. }
  194. print("M4_PH",M4_PH);
  195. HAL_Delay(10000);
  196. }
  197. void MX_ADC1_Init1(char pin)
  198. {
  199. ADC_ChannelConfTypeDef sConfig = {0};
  200. hadc1.Instance = ADC1;
  201. hadc1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV2;
  202. hadc1.Init.Resolution = ADC_RESOLUTION_12B;
  203. hadc1.Init.ScanConvMode = DISABLE;
  204. hadc1.Init.ContinuousConvMode = DISABLE;
  205. hadc1.Init.DiscontinuousConvMode = DISABLE;
  206. hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
  207. hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
  208. hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
  209. hadc1.Init.NbrOfConversion = 1;
  210. hadc1.Init.DMAContinuousRequests = DISABLE;
  211. hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
  212. if (HAL_ADC_Init(&hadc1) != HAL_OK)
  213. {
  214. Error_Handler();
  215. }
  216. sConfig.Channel = pin;
  217. sConfig.Rank = 1;
  218. sConfig.SamplingTime = ADC_SAMPLETIME_3CYCLES;
  219. if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
  220. {
  221. Error_Handler();
  222. }
  223. }
  224. void MX_GPIO_Input1(unsigned long pin)
  225. {
  226. GPIO_InitTypeDef GPIO_InitStruct = {0};
  227. __HAL_RCC_GPIOE_CLK_ENABLE();
  228. GPIO_InitStruct.Pin = pin;
  229. GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  230. GPIO_InitStruct.Pull = GPIO_PULLDOWN;
  231. HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  232. }
  233. void Error_Handler(void)
  234. {
  235. }